﻿using System;
using FunctionalCLR.Core;
using FunctionalCLR.Shell;
using Zove.Numerics.Core.Function;

namespace Zove.Desktop.Function {

    /// <summary>
    /// plot("x+2", 0, 100, x+2, x);
    /// </summary>
    public class PlotNode : ContextFuncNode {

        public PlotNode()
            : base() {
            base.Value = "plot";
        }

        public override FuncNode GetReversedFunction(int index) {
            throw new NotImplementedException();
        }

        public override object Evaluate() {
            int count = Children.Count;

            string title = Children[count - 1].Evaluate().ToString();
            double from = Convert.ToDouble(Children[count - 2].Evaluate());
            double to = Convert.ToDouble(Children[count - 3].Evaluate());

            string expr = string.Empty;
            if (Children[count - 4] is SolutionNode) {
                expr = Children[count - 4].Evaluate().ToString();
            } else {
                CSharpCodeTranslator csct 
                    = new CSharpCodeTranslator(Context.GetFunctionManager());
                expr = csct.Translate(Children[count - 4]);
            }
            string var = Children[count - 5].Value;
            IUnaryFunction func = this.Context.UnaryFunctionFactory.Create(var, expr);

            PlottingForm pf = new PlottingForm(
                title, "x", "y", var, from, to, func);
            pf.MdiParent = DesktopFormContext.Context.Main;
            pf.Show();

            return string.Empty;
        }

    }
}
